﻿using HKH.Mef2.Integration;

using EIMSNext.Core;
using EIMSNext.Core.Service;
using EIMSNext.Entity;
using EIMSNext.Service.Interface;

using MongoDB.Driver;
using System.Threading.Tasks;

namespace EIMSNext.Service
{
    public class FormDefService(IResolver resolver) : EntityServiceBase<FormDef>(resolver), IFormDefService
    {
        protected override async Task AfterAdd(IEnumerable<FormDef> entities, IClientSessionHandle? session)
        {
            await base.AfterAdd(entities, session);
            var appRepo = Resolver.GetRepository<App>();
            var app = appRepo.Get(entities.First().AppId, session)!;
            var maxIndex = app.AppMenus.Count == 0 ? 0 : app.AppMenus.Max(x => x.SortIndex);
            entities.ForEach(e =>
            {
                maxIndex = maxIndex + 100;
                app.AppMenus.Add(new AppMenu { MenuId = e.Id, Icon = "", IconColor = "", MenuType = e.Type, Title = e.Name, SortIndex = maxIndex });
            });
            appRepo.Replace(app, session);

            return;
        }

        protected override async Task AfterDelete(FilterDefinition<FormDef> filter, IClientSessionHandle? session)
        {
             await base.AfterDelete(filter, session);
            // 找到被删除的 FormDef 实体
            var deletedForms = await Collection.Find(filter).ToListAsync();
            if (deletedForms.Count == 0)
                return;

            var appRepo = Resolver.GetRepository<App>();

            // 按 AppId 分组，批量处理
            var appIds = deletedForms.Select(f => f.AppId).Distinct();
            foreach (var appId in appIds)
            {
                var app = appRepo.Get(appId, session);
                if (app == null) continue;

                // 移除所有被删除 FormDef 对应的菜单
                var removedCount = app.AppMenus.RemoveAll(m => deletedForms.Any(f => f.Id == m.MenuId));
                if (removedCount > 0)
                {
                    appRepo.Replace(app, session);
                }
            }

            return;
        }
       
    }
}
